package org.edu.modules.sxjy.controller;

import com.alibaba.fastjson.JSON;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.modules.sxjy.entity.VeBaseBanJi;
import org.edu.modules.sxjy.feign.VeCommonClient;
import org.edu.modules.sxjy.feign.VeEnrollApiClient;
import org.edu.modules.sxjy.feign.VeJwJxgzApiClient;
import org.edu.modules.sxjy.utils.DateUtils;
import org.edu.modules.sxjy.vo.StuSpecCountVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags={"学生专业信息"})
@RestController
@RequestMapping({"/sxjy/stuSpecInfo"})
@ApiSort(60)
public class VeStuSpecInfoController
{
    private static final Logger log = LoggerFactory.getLogger(VeStuSpecInfoController.class);
    @Autowired
    private VeEnrollApiClient veEnrollApiClient;
    @Autowired
    private VeJwJxgzApiClient veJwJxgzApiClient;
    @Autowired
    private VeCommonClient veCommonClient;

    @AutoLog("学生专业信息-查询列表")
    @ApiOperation(value="查询列表", notes="学生专业信息-查询列表")
    @GetMapping({"/selectAll"})
    public Result<?> selectAll(@RequestParam(name="specName", required=false) String specName, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize)
    {
        return this.veCommonClient.getGraduatePeopleCount("09d5e1e7f9b049008eee645c783a1d67", specName);
    }

    @AutoLog("学生专业信息-查询列表")
    @ApiOperation(value="查询列表", notes="学生专业信息-查询列表")
    @GetMapping({"/queryKcInfoBySpecList"})
    public Result<?> queryKcInfoBySpecList(@RequestParam("specId") String specId, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize)
    {
        return this.veJwJxgzApiClient.queryKcInfoBySpecList(specId, pageNo, pageSize);
    }

    @ApiOperation("根据专业统计学生招生数据")
    @PostMapping({"/getStuInfoByspecId"})
    public Result<?> getStuInfoByspecId(@ApiParam("专业id") @RequestParam("specId") Integer specId, @ApiParam("开始时间") @RequestParam("startTime") String startTime, @ApiParam("结束时间") @RequestParam("endTime") String endTime)
    {
        return this.veEnrollApiClient.getStuInfoByspecId(specId, startTime, endTime);
    }

    @AutoLog("根据专业统计学生毕业数据")
    @ApiOperation(value="根据专业统计学生毕业数据", notes="根据专业统计学生毕业数据")
    @GetMapping({"/getStuGraduateInfoByspecId"})
    public Result<?> getStuGraduateInfoByspecId(@RequestParam("specId") Integer specId, @ApiParam("开始时间") @RequestParam("startTime") String startTime, @ApiParam("结束时间") @RequestParam("endTime") String endTime)
            throws ParseException
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date startDate = sdf.parse(startTime);
        Date endDate = sdf.parse(endTime);

        Calendar calendarStartTime = Calendar.getInstance();
        calendarStartTime.setTime(startDate);

        Calendar calendarEndTime = Calendar.getInstance();
        calendarEndTime.setTime(endDate);


        int startYear = calendarStartTime.get(1);
        int endYear = calendarEndTime.get(1);

        List<VeBaseBanJi> veBaseBanJiList = new ArrayList();
        Result<?> result = this.veCommonClient.queryBanJiListBySpecId("09d5e1e7f9b049008eee645c783a1d67", specId);
        List<Object> bjResultList = (List)result.getResult();
        for (Object o : bjResultList)
        {
            String s = JSON.toJSONString(o);
            VeBaseBanJi veBaseBanJi = (VeBaseBanJi)JSON.parseObject(s, VeBaseBanJi.class);
            veBaseBanJiList.add(veBaseBanJi);
        }
        //lbh2024/07/31 Object >> List<StuSpecCountVo>
        List<StuSpecCountVo> stuSpecCountList = new ArrayList();
        for (int i = startYear; i <= endYear; i++)
        {
            StuSpecCountVo stuSpecCount = new StuSpecCountVo();
            stuSpecCount.setYear(Integer.valueOf(i));
            stuSpecCount.setPeopleCount(Integer.valueOf(0));
            //lbh2024/07/31 (List)stuSpecCountList >> stuSpecCountList
            stuSpecCountList.add(stuSpecCount);
        }
        //lbh2024/07/31 (List)stuSpecCountList >> stuSpecCountList
        for (StuSpecCountVo stuSpecCountVo : stuSpecCountList)
        {
            int sum = 0;
            for (VeBaseBanJi veBaseBanJi : veBaseBanJiList) {
                if ((veBaseBanJi.getBysj() != null) && (!"".equals(veBaseBanJi.getBysj())))
                {
                    String dateToString = DateUtils.timestampToDate(veBaseBanJi.getBysj().intValue());
                    Date dateToStr = sdf.parse(dateToString);
                    calendarStartTime.setTime(dateToStr);
                    int year = calendarStartTime.get(1);
                    if (stuSpecCountVo.getYear().equals(Integer.valueOf(year))) {
                        sum += veBaseBanJi.getZrs().intValue();
                    }
                }
            }
            stuSpecCountVo.setPeopleCount(Integer.valueOf(sum));
        }
        return Result.OK(stuSpecCountList);
    }
}
